﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll.AIR;
using EZCRS.HCMS.SqlClient;
using EZCRS.HCMS.DataAcl;

namespace EZCRS.HCMS.Bll.revenue
{
    public class RMS
    {

        private double _dRevenue = 0f;

        private int _iSoldRooms = 0;
        private int _iInventory = 0;

        private double _dOCC = 0f;
        private double _dADR = 0f;

        private double _dRevPAR = 0f;

        /// <summary>
        /// 获取酒店总房量
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <returns></returns>
        private int GetInventory(int HotelID, int Year, int Month)
        {
            sql_hcrs_air airDO = new sql_hcrs_air();

            List<HCRS_AIR> lstSearch = airDO.getall("year = " + Year.ToString() + " AND Property_id = " + HotelID + " AND Level = 'R'");

            int iAmount = 0;

            if (lstSearch != null)
            {
                foreach (HCRS_AIR air in lstSearch)
                {

                    if (air.Level == "R")
                    {
                        //只计算房型级

                        HotelAIProvider prov = new HotelAIProvider();

                        prov.Load(air);

                        iAmount += prov.GetInventoryAmount(Month);

                    }
                }
            }

            return iAmount;

        }

        /// <summary>
        /// 执行计算，填充数值
        /// </summary>
        /// <param name="HotelID"></param>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        public void Execute(int HotelID, int Year, int Month)
        {
            HotelReport hr = new HotelReport();

            DateTime date1 = new DateTime();
            DateTime date2 = new DateTime();

            date1 = Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-1");

            date2 = date1.AddMonths(1).AddDays(-1);

            hr.RoomData(HotelID, date1, date2, out _iSoldRooms, out _dRevenue);

            _iInventory = GetInventory(HotelID, Year, Month);

            if(_iInventory > 0)
                _dOCC = (double)_iSoldRooms / (double)_iInventory * 100;  //入住率

            if(_iSoldRooms > 0)
                _dADR = _dRevenue / (double)_iSoldRooms;    //平均房价，(P*S) / S

            _dRevPAR = _dADR * _dOCC;        //RevPAR

        }

        /// <summary>
        /// 出租房
        /// </summary>
        public int SoldRooms
        {
            get
            {
                return _iSoldRooms;
            }
        }

        /// <summary>
        /// 房间总收入
        /// </summary>
        public double Revenue
        {
            get
            {
                return _dRevenue;
            }
        }

        /// <summary>
        /// 总房量
        /// </summary>
        public int Inventory
        {
            get
            {
                return _iInventory;
            }
        }

        public double OCC
        {
            get
            {
                return _dOCC;
            }
        }

        public double ADR
        {
            get
            {
                return _dADR;
            }
        }

        public double RevPAR
        {
            get
            {
                return _dRevPAR;
            }
        }

    }
}
